package org.steven.crm.app.api.processor;

import org.steven.crm.app.api.processor.base.BaseProcessor;
import org.apache.commons.io.FileUtils;
import org.steven.crm.core.exception.ServiceException;
import org.steven.crm.core.service.system.SystemFileService;
import org.steven.crm.core.utils.FileUtil;
import org.steven.crm.core.utils.ImageUtil;

import java.io.File;
import java.util.Map;

/**
 * @Author wuzw
 * @Date 2023/5/20 20:05
 * @Version 1.0
 */
public class CommonProcessor extends BaseProcessor {
    private static SystemFileService systemFileService = new SystemFileService();

    public void uploadImageAction() throws Exception {
        File uploadFile = getUploadFile("uploadFile");
        if (uploadFile == null) {
            FileUtils.forceDelete(uploadFile);
            throw new ServiceException("请上传图片");
        }
        String fileFormat = FileUtil.getFileFormat(uploadFile);
        if (!ImageUtil.isImageFile(fileFormat)) {
            FileUtils.forceDelete(uploadFile);
            throw new ServiceException("图片格式不正确");
        }

        Map<String, String> systemFile = systemFileService.upload(uploadFile, getStringData("originalName"));
        setObjectData(systemFile);
    }

    public void uploadFileAction() throws Exception {
        File uploadFile = getUploadFile("uploadFile");
        if (uploadFile == null) {
            FileUtils.forceDelete(uploadFile);
            throw new ServiceException("请上传文件");
        }
        Map<String, String> systemFile = systemFileService.upload(uploadFile, getStringData("originalName"));
        setObjectData(systemFile);
    }
}
